package com.wyu.notaryserver.controller;

import com.wyu.notaryserver.DTO.IdCardDTO;
import com.wyu.notaryserver.common.ResponseResult;
import com.wyu.notaryserver.domain.*;
import com.wyu.notaryserver.service.OcrService;
import com.wyu.notaryserver.service.SubmitService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 六味lemontea 2025-01-05
 * @version 1.0
 * @description 前端小程序控制器
 */
@RestController
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private OcrService ocrService;
    @Autowired
    private SubmitService submitService;
    /**
     * @description 返回给前端需要的清单。
     * @return
     */
    @GetMapping("/mould")
    public ResponseResult mould(@RequestParam String query_code,@RequestParam String token){

        MouldVO mould = ocrService.getMould(token);
        return ResponseResult.okResult(mould);
    }

    /**
     *
     * @param
     * @return
     * @description 提交上传所有图片的url。
     */
    @Operation(summary = "提交上传所有图片的url")
    @PostMapping("/dataSave")
    public ResponseResult dataSave(@RequestBody IdCardDTO idCardDTO) throws IOException, InterruptedException {
        System.out.println(idCardDTO);
        submitService.saveData(idCardDTO);
        return ResponseResult.okResult();
    }
    /**
     *
     * @param
     * @return
     * @description 上传的图片进行文字识别(需要回显给用户进行确认)
     */

    //todo Switch处理不同业务
    @PostMapping("/OCR")
    public ResponseResult ocrQuery(@RequestBody FileRequestList files) throws IOException {
        String licenseName = files.getName();
        // 提取文件数据到字符串数组
        int flag = 0;
        String[] base64Files = new String[files.getFiles().length];
        List<String> uuidList = new ArrayList<>();
        for (FileRequest file : files.getFiles()) {
            base64Files[flag] = file.getFile();
            uuidList.add(file.getUuid());
            flag++;
        }
        List<CommonDTO> commonDTOList = ocrService.fileUpload(base64Files,uuidList,licenseName);
        Ocr ocr = new Ocr();
        ocr.setOcr(commonDTOList);
        return ResponseResult.okResult(ocr);
    }
    /**
     *
     * @param
     * @return
     * @description 上传的图片进行保存前端
     *//*
    @PostMapping("/OCR/save")
    public ResponseResult ocrSave(@RequestBody ClientDTO  clientDTO) throws IOException {
        ocrService.fileSave(clientDTO);
        return ResponseResult.okResult();
    }*/
}
